import {
  login,
  logout,
  getUserInfo
} from '@/api'
import {
  getToken,
  setToken,
  removeToken,
  getUid,
  setUid,
  removeUid,
  getNumber,
  setNumber,
  removeNumber,
  getPASSWORD_KEY,
  setPASSWORD_KEY,
  removePASSWORD_KEY,
  getUSERNAME_KEY,
  setUSERNAME_KEY,
  removeUSERNAME_KEY
} from '../../utils/auth'
import {
  resetRouter
} from '@/router'


const state = {
  token: getToken(),
  name: '',
  avatar: '',
  number: '',
  uid: getUid(),
  router: [],
  username: '',
  password: '',
  user_detail_id: '',
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_UID: (state, uid) => {
    state.uid = uid
  },
  SET_Router: (state, router) => {
    state.router = router
  },
  SET_NUMBER: (state, number) => {
    state.number = number
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_USERNAME: (state, username) => {
    state.username = username
  },
  SET_PASSWORD: (state, password) => {
    state.password = password
  },
  set_user_detail_id: (state, user_detail_id) => state.user_detail_id = user_detail_id,
}

const actions = {
  // user login
  login({
    commit
  }, input) {
    const {
      username,
      password
    } = input
    return new Promise((resolve, reject) => {
      login({
        username: username.trim(),
        password: password
      }).then(res => {
        console.log('login:', res.data)
        const {
          token,
          id,
          userDetailId,
          router,
          staffNumber,
        } = res.data
        localStorage.setItem('staffNumber', staffNumber)
        commit('set_user_detail_id', userDetailId)
        commit('SET_TOKEN', token)
        commit('SET_UID', userDetailId)
        commit('SET_Router', router)
        commit('SET_USERNAME', username.trim())
        commit('SET_PASSWORD', password)
        commit('SET_NUMBER', staffNumber)
        setUid(userDetailId)
        setToken(token)
        setPASSWORD_KEY(password)
        setUSERNAME_KEY(username.trim())

        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getUserInfo({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      // getUserInfo(state.uid).then(response => {
      //   const {
      //     data
      //   } = response

      //   if (!data) {
      //     reject('验证失败，请重新登陆')
      //   }

      const {
        name,
        number
      } = data

      commit('SET_NAME', 'cxt')
      commit('SET_NUMBER', '吃屎')
      setNumber('01928098192')

      // }).catch(error => {
      //   reject(error)
      // })
    })
  },

  // user logout
  logout({
    commit,
    state,
    dispatch,
  }) {
    return new Promise((resolve, reject) => {
      logout(state.uid).then(() => {
        dispatch('clear')
        resetRouter()
        removeToken()
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  clear({
    commit
  }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_UID', '')
      commit('SET_NAME', '')
      commit('SET_NUMBER', '')
      removeUid()
      removeToken()
      removeNumber()
      localStorage.removeItem('username')
      // removeRouter()
      removeUSERNAME_KEY()
      removeUSERNAME_KEY()
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
